import 'dart:convert';

import 'package:shared_preferences/shared_preferences.dart';

class XmmSPUtil {
  static SharedPreferences? _prefs;

  static init() async {
    _prefs = await SharedPreferences.getInstance();
  }

  static void set(String key, dynamic value) {
    if (value is int) {
      _prefs?.setInt(key, value);
    } else if (value is String) {
      _prefs?.setString(key, value);
    } else if (value is bool) {
      _prefs?.setBool(key, value);
    } else if (value is double) {
      _prefs?.setDouble(key, value);
    } else if (value is List<String>) {
      _prefs?.setStringList(key, value);
    } else {
      _prefs?.setString(key, jsonEncode(value));
    }
  }

  static dynamic get(String key, {dynamic defValue}) {
    return _prefs?.get(key) ?? defValue;
  }

  static remove(String key) {
    return _prefs?.remove(key);
  }
}
